package com.sjgame.aspect;

import com.sjgame.base.entity.User;
import com.sjgame.base.service.IResourceService;
import com.sjgame.model.entity.Log;
import com.sjgame.model.service.LogService;
import org.apache.shiro.SecurityUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;


@Aspect
@Component
public class HttpAspect {
    @Autowired
    private LogService logService;
    private final static Logger logger = LoggerFactory.getLogger(HttpAspect.class);

    @Pointcut("execution(public * com.sjgame.base.controller.*.*(..))")
    public void log() {
    }

    @Before("log()")
    public void doBefore(JoinPoint joinPoint) {
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//         user= (User) SecurityUtils.getSubject().getPrincipal();
//        //url
//        logger.info("url={}", request.getRequestURL());
//        //method
//        logger.info("method={}", request.getMethod());
//        //ip
//        logger.info("ip={}", request.getRemoteAddr());
//        //类方法
//        logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
//        //参数
//        logger.info("args={}", joinPoint.getArgs());
    }

    @After("log()")
    public void doAfter(JoinPoint joinPoint) {
//        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        HttpServletRequest request = attributes.getRequest();
//        user= (User) SecurityUtils.getSubject().getPrincipal();
//        //url
//        logger.info("url={}", request.getRequestURL());
//        //method
//        logger.info("method={}", request.getMethod());
//        //ip
//        logger.info("ip={}", request.getRemoteAddr());
//        //类方法
//        logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
//        //参数
//        logger.info("args={}", joinPoint.getArgs());
    }

    @AfterReturning(returning = "object", pointcut = "log()")
    public void doAfterReturning(JoinPoint joinPoint,Object object) {

        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            User user= (User) SecurityUtils.getSubject().getPrincipal();

            if(user!=null && "post".equalsIgnoreCase(request.getMethod()) ){
                Log log = new Log();
                log.setUserName(user.getUserName());
                log.setNickName(user.getNickName());
                log.setIp(request.getRemoteAddr());
                log.setMethod(joinPoint.getSignature().getName());
                log.setCreateTime(new Date());
                log.setResult(object.toString());
                log.setArgs(joinPoint.getArgs()[0].toString());
                logService.save(log);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
      /*  logger.info("response={}", object.toString());
        //url
        logger.info("url={}", request.getRequestURL());
        //method
        logger.info("method={}", request.getMethod());
        //ip
        logger.info("ip={}", request.getRemoteAddr());
        //类方法
        logger.info("class_method={}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        //参数
        logger.info("args={}", joinPoint.getArgs());*/
    }

}
